package com.lanier.dydownloader

import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.junit.Assert.*
import org.junit.Test
import us.codecraft.xsoup.Xsoup


/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
class ExampleUnitTest {
    @Test
    fun addition_isCorrect() {
        assertEquals(4, 2 + 2)
    }

    @Test
    fun testSelect() {
        val html = """
            <html>
            
            <div><a href='https://github.com'>github.com</a></div>
             <table><tr><td>a</td><td>b</td></tr></table>
                
           </html>
        """.trimIndent()
        val document: Document = Jsoup.parse(html)
        val result = Xsoup.compile("//a/@href").evaluate(document).get()
        assertEquals("https://github.com", result)
        println("信息: result:$result")

        val list = Xsoup.compile("//tr/td/text()").evaluate(document).list()
        println("信息: list:[${list.map { "$it," }}]")
        assertEquals("a", list[0])
        assertEquals("b", list[1])

    }

    @Test
    fun testSelectDouYin() {
//        val awesomeUrl = ParseUtil.getAwesomeUrl("https://v.douyin.com/idoG6UTD/")
//        println("信息: awesomeUrl:$awesomeUrl")

//        document = Jsoup.parse(awesomeUrl)
//        println("信息: document:$document")
        // data-e2e="scroll-list"
        // "//ul[contains(@data-e2e,'scroll-list')]"
//        val list = Xsoup.compile("//a[contains(@href,'/video/')]").evaluate(document).list()
//        println("信息: list size:${list.size}")
//        list.forEach {
//            val result = Xsoup.compile("//a/@href").evaluate(Jsoup.parse(it)).get()
//            if (result.contains("https://").not()) println("信息: 单个Url:https://v.douyin.com$result")
//        }


//        val data = ParseUtil.getData(awesomeUrl)
//        println("信息: data:${data}")
//        val postInfo = ParseUtil.getPostInfo(data)
//        println("信息: postInfo:${postInfo}")

//        val awesomeInfo = ParseUtil.getAwesomeInfo(data)
//        println("信息: awesomeInfo:${awesomeInfo}")


    }

    @Test
    fun testGetJsonStr() {
        // document.getElementsByTagName('body')[0].innerHTML;
//        val document = Jsoup.parse(T.bodyHtml)
//        println("信息: document:$document")
//        var list = Xsoup.compile("//a[contains(@href,'/video/')]").evaluate(document).list()
//
        //*[@id="douyin-right-container"]/div[2]/div/div/div[3]/div/div/div[2]/div[2]/div[2]
//        val list = Xsoup.compile("//*[@id=\"douyin-right-container\"]/div[2]/div/div/div[3]/div/div/div[2]/div[2]/div[2]").evaluate(document).list()
//
//        println("信息: list size:${list.size}")
//        list.forEach {
//            val result = Xsoup.compile("//a/@href").evaluate(Jsoup.parse(it)).get()
//            if (result.contains("https://").not()) println("信息: 单个Url:https://www.douyin.com$result")
//        }
//
//
//        // 字符串匹配
//        if (list.isEmpty()) {
//            list = T.findAllInText(T.bodyHtml)
//        }
//
//        println("信息: list size:${list.size}")
//        list.forEach {
//            println("信息: list item:${it}")
////            val result = Xsoup.compile("//a/@href").evaluate(Jsoup.parse(it)).get()
////            if (result.contains("https://").not()) println("信息: 单个Url:https://www.douyin.com$result")
//        }

    }
}